package com.newasia.server.common.factory;

import com.newasia.server.common.template.AnalysisTemplateClassLoader;
import com.newasia.server.common.template.AnalysisTemplateTool;
import com.newasia.server.common.template.ConfigReader;
import com.newasia.server.common.template.ModuleAnalysisTemplate;
import com.newasia.server.common.template.dataItem.ProtocolFormat;

import java.util.HashMap;
import java.util.Map;

public class ModuleFactory {

	private ModuleFactory(){}

    private static Map<String,ModuleAnalysisTemplate> map = new HashMap<String,ModuleAnalysisTemplate>();

    public static ModuleAnalysisTemplate getInstance(Long moduleId){
    	String className = "NA"+moduleId;
        synchronized(map){
            if(!map.containsKey(className)){
                ModuleAnalysisTemplate template = null;
                try{
                    ProtocolFormat protocolFormat = ConfigReader.doLoadConfig(moduleId);
                    template = (ModuleAnalysisTemplate) AnalysisTemplateTool.newInstance(new AnalysisTemplateClassLoader(),protocolFormat);
                    template.setProtocolFormat(protocolFormat);
                	map.put(className,template);
                }catch (Exception e){
                    e.printStackTrace();
                }
                return template;
            }
            return map.get(className);
        }
    }
}
